package com.fizzkai.p2p.core.service.impl;

import com.fizzkai.p2p.bussness.domain.ExpAccount;
import com.fizzkai.p2p.bussness.service.IExpAccountService;
import com.fizzkai.p2p.core.domain.Account;
import com.fizzkai.p2p.core.domain.IpLog;
import com.fizzkai.p2p.core.domain.LoginInfo;
import com.fizzkai.p2p.core.domain.UserInfo;
import com.fizzkai.p2p.core.exception.DisplayableException;
import com.fizzkai.p2p.core.mapper.LoginInfoMapper;
import com.fizzkai.p2p.core.service.IAccountService;
import com.fizzkai.p2p.core.service.IIpLogService;
import com.fizzkai.p2p.core.service.ILoginInfoService;
import com.fizzkai.p2p.core.service.IUserInfoService;
import com.fizzkai.p2p.core.service.IVerifyCodeService;
import com.fizzkai.p2p.core.util.AssertUtil;
import com.fizzkai.p2p.core.util.Constants;
import com.fizzkai.p2p.core.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional // 开启 事务
public class LoginInfoServiceImpl implements ILoginInfoService {
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Autowired
    private IVerifyCodeService verifyCodeService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IExpAccountService expAccountService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IIpLogService ipLogService;


    @Override
    public void register(String username, String password, String verifycode, String confirmPwd) {
        // 断言 判断
        AssertUtil.isNotNull(username, "用户名不能为空");
        AssertUtil.isNotNull(password, "密码不能为空");
        AssertUtil.isNotNull(verifycode, "验证码不能为空");
        AssertUtil.isNotNull(confirmPwd, "确认密码不能为空");
        AssertUtil.isEquals(password, confirmPwd, "两次密码输入不一致");
        // 判断 验证码 TODO
        if (!verifyCodeService.validate(username, verifycode)) {
            throw new DisplayableException("验证码有误，请重新输入！！");
        }
        // 判断 用户 是否存在
        int count = loginInfoMapper.selectByUsername(username);
        if (count > 0) {
            throw new DisplayableException("用户名 已存在");
        }
        // 注册
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername(username);
        loginInfo.setPassword(password);
        loginInfo.setUserType(LoginInfo.USERTYPE_USER);
        loginInfoMapper.insert(loginInfo);
        // 创建 关联对象 Account / UserInfo
        Account account = new Account();
        account.setId(loginInfo.getId());
        accountService.save(account);

        UserInfo userInfo = new UserInfo();
        userInfo.setId(loginInfo.getId());
        userInfo.setPhoneNumber(username);
        userInfoService.save(userInfo);

        // 创建 关联对象 ExpAccount
        ExpAccount expAccount = new ExpAccount();
        expAccount.setId(loginInfo.getId());
        expAccountService.save(expAccount);
        // 发放 体验金
        expAccountService.grantExpMoney(expAccount.getId(), Constants.REGISTER_GRANT_EXPMONEY, new IExpAccountService.LastTime(1, IExpAccountService.LastTimeUnit.MONTH), Constants.EXPMONEY_TYPE_REGISTER);


    }

    @Override
    public boolean checkPhone(String username) {
        return loginInfoMapper.selectByUsername(username) < 1;
    }

    @Override
    public LoginInfo login(String username, String password,int userType) {
        AssertUtil.isNotNull(username, "用户名不能为空");
        AssertUtil.isNotNull(password, "密码不能为空");
        LoginInfo login = loginInfoMapper.selectByLogin(username, password, userType);
        // 记录 日志
        IpLog ipLog = new IpLog();
        ipLog.setLoginTime(new Date());
        ipLog.setIp(UserContext.getIp());
        ipLog.setUsername(username);
        // 登录 设置进 session, 设置 状态
        if (login != null) {
            UserContext.setCurrentLoginInfo(login);
            ipLog.setState(IpLog.STATE_SUCCESS);
        } else {
            ipLog.setState(IpLog.STATE_FAILED);
        }
        ipLogService.save(ipLog);
        return login;
    }

    @Override
    public void initAdmin() {
        int count = loginInfoMapper.selectCountByUserType(LoginInfo.USERTYPE_MANAGER);
        if (count > 0) {
            return;
        }
        // 没有就 创建
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername(Constants.DEFAULT_ADMIN_ACCOUNT);
        loginInfo.setPassword(Constants.DEFAULT_ADMIN_PASSWORD);
        loginInfo.setUserType(LoginInfo.USERTYPE_MANAGER);
        loginInfoMapper.insert(loginInfo);
    }

    @Override
    public List<LoginInfo> selectIsAuditorList() {
        return loginInfoMapper.selectIsAuditorList(LoginInfo.USERTYPE_MANAGER, LoginInfo.ISAUDITOR_YES);
    }

    @Override
    public LoginInfo get(Long id) {
        return loginInfoMapper.selectByPrimaryKey(id);
    }
}
